package com.gymmsystem.GYMMSystem.controller;

import com.gymmsystem.GYMMSystem.entity.MemberCardType;
import com.gymmsystem.GYMMSystem.service.MemberCardTypeService;
import com.gymmsystem.GYMMSystem.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 会员卡类型管理控制器
 */
@RestController
@RequestMapping("/api/member-card-type")
@CrossOrigin(origins = "${cors.allowed-origins}", allowCredentials = "true")
public class MemberCardTypeController {

    @Autowired
    private MemberCardTypeService memberCardTypeService;

    /**
     * 获取所有会员卡类型
     */
    @GetMapping("/list")
    public Result<List<MemberCardType>> getAllCardTypes() {
        try {
            List<MemberCardType> list = memberCardTypeService.list();
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("获取会员卡类型列表失败");
        }
    }

    /**
     * 获取可用的会员卡类型
     */
    @GetMapping("/available")
    public Result<List<MemberCardType>> getAvailableCardTypes() {
        try {
            List<MemberCardType> list = memberCardTypeService.getAvailableCardTypes();
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("获取可用会员卡类型失败");
        }
    }

//    /**
//     * 根据ID获取会员卡类型
//     */
//    @GetMapping("/{id}")
//    public Result<MemberCardType> getCardTypeById(@PathVariable Long id) {
//        try {
//            MemberCardType cardType = memberCardTypeService.getById(id);
//            if (cardType == null) {
//                return Result.error("会员卡类型不存在");
//            }
//            return Result.success(cardType);
//        } catch (Exception e) {
//            return Result.error("获取会员卡类型失败");
//        }
//    }
//
//    /**
//     * 根据名称获取会员卡类型
//     */
//    @GetMapping("/name/{cardName}")
//    public Result<MemberCardType> getCardTypeByName(@PathVariable String cardName) {
//        try {
//            MemberCardType cardType = memberCardTypeService.getByCardName(cardName);
//            if (cardType == null) {
//                return Result.error("会员卡类型不存在");
//            }
//            return Result.success(cardType);
//        } catch (Exception e) {
//            return Result.error("获取会员卡类型失败");
//        }
//    }

    /**
     * 更新会员卡价格
     */
    @PutMapping("/{id}/price")
    public Result<String> updateCardPrice(@PathVariable Long id, @RequestParam BigDecimal price) {
        try {
            boolean success = memberCardTypeService.updateCardPrice(id, price);
            return success ? Result.success("价格更新成功") : Result.error("价格更新失败");
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("价格更新失败");
        }
    }

    /**
     * 更新会员卡状态
     */
    @PutMapping("/{id}/status")
    public Result<String> updateCardStatus(@PathVariable Long id, @RequestParam Integer status) {
        try {
            boolean success = memberCardTypeService.updateCardStatus(id, status);
            return success ? Result.success("状态更新成功") : Result.error("状态更新失败");
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("状态更新失败");
        }
    }

    /**
     * 更新会员卡价格和状态
     */
    @PutMapping("/{id}")
    public Result<String> updateCardPriceAndStatus(@PathVariable Long id,
                                                   @RequestParam BigDecimal price,
                                                   @RequestParam Integer status) {
        try {
            boolean success = memberCardTypeService.updateCardPriceAndStatus(id, price, status);
            return success ? Result.success("更新成功") : Result.error("更新失败");
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("更新失败");
        }
    }

    /**
     * 创建会员卡类型
     */
    @PostMapping
    public Result<String> createCardType(@RequestBody MemberCardType cardType) {
        try {
            // 检查名称是否重复
            MemberCardType existing = memberCardTypeService.getByCardName(cardType.getCardName());
            if (existing != null) {
                return Result.error("会员卡类型名称已存在");
            }

            boolean success = memberCardTypeService.save(cardType);
            return success ? Result.success("创建成功") : Result.error("创建失败");
        } catch (Exception e) {
            return Result.error("创建会员卡类型失败");
        }
    }

    /**
     * 更新会员卡类型信息
     */
    @PutMapping("/update")
    public Result<String> updateCardType(@RequestBody MemberCardType cardType) {
        try {
            if (cardType.getCardTypeId() == null) {
                return Result.error("卡类型ID不能为空");
            }

            boolean success = memberCardTypeService.updateById(cardType);
            return success ? Result.success("更新成功") : Result.error("更新失败");
        } catch (Exception e) {
            return Result.error("更新会员卡类型失败");
        }
    }

    /**
     * 删除会员卡类型
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteCardType(@PathVariable Long id) {
        try {
            boolean success = memberCardTypeService.removeById(id);
            return success ? Result.success("删除成功") : Result.error("删除失败");
        } catch (Exception e) {
            return Result.error("删除会员卡类型失败");
        }
    }

    /**
     * 初始化默认会员卡类型
     */
    @PostMapping("/init-default")
    public Result<String> initDefaultCardTypes() {
        try {
            memberCardTypeService.initDefaultCardTypes();
            return Result.success("默认会员卡类型初始化成功");
        } catch (Exception e) {
            return Result.error("初始化失败");
        }
    }
}